﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Configuration;

namespace FB.Providers
{
    public class AspNetEmployeeInfoProviderManager
    {
        static AspNetEmployeeInfoProviderManager()
        {
            Initialize();
        }


        static AspNetEmployeeInfoProvider _default;
        public static AspNetEmployeeInfoProvider Default
        {
            get { return _default; }
        }


        static AspNetEmployeeInfoProviderCollection _providerCollection;
        public static AspNetEmployeeInfoProviderCollection Providers
        {
            get { return _providerCollection; }
        }


        static ProviderSettingsCollection _providerSettings;
        public static ProviderSettingsCollection ProviderSettings
        {
            get { return _providerSettings; }
        }


        static void Initialize()
        {
			var configSection = (AspNetEmployeeInfoProviderConfiguration)ConfigurationManager.GetSection(@"system.web/employeeManager");
            if (configSection == null)
                throw new ConfigurationException("employee info provider section is not set.");

            _providerCollection = new AspNetEmployeeInfoProviderCollection();
            ProvidersHelper.InstantiateProviders(configSection.Providers, _providerCollection, typeof(AspNetEmployeeInfoProvider));

            _providerSettings = configSection.Providers;

            if (configSection.Enabled)
            {
                if (_providerCollection[configSection.Default] == null)
                    throw new ConfigurationException("Default provider is not set.");

                _default = _providerCollection[configSection.Default];
            }
        }

    }
}
